package polyu.comp;

import java.text.SimpleDateFormat;
import java.util.*;

import com.google.android.maps.*;

/***
 * 
 * @author JasonHe
 *
 * An Action Item is a single run event.
 */
public class ActionItem {


	public String 		uid;		// User ID
	public Date			start;		// Run Start Time
	public Date			end;		// Run End Time
	private Date		dbcur;		// Time that logged to database, NULL means never post to server
	public int			length;		// Length of the running
	// TODO: Add GeoPoint here.
	// public GeoPoint		trace;		// The trace information of the running
	
	// Constructor
	public ActionItem(){
	}
	
	public ActionItem(String uid, Date start, Date end, Date dbcur, int length) {
		this.uid = uid;
		this.start = start;
		this.end = end;
		this.dbcur = dbcur;
		this.length = length;
	}
	
	public ActionItem(String uid, long start, long end, long dbcur, int length) {
		this.uid = uid;
		this.start = new Date(start * 1000);
		this.end = new Date(end * 1000);;
		this.dbcur = new Date(dbcur * 1000);;
		this.length = length;
	}
	
	// Construct from Network and File
	// tom,1333174320,1333177937,1332745920,874
	public ActionItem(String arg) {
		String [] param = arg.split(",");
		this.uid = param[0];
		this.start = new Date(Long.parseLong(param[1]) * 1000);
		this.end = new Date(Long.parseLong(param[2]) * 1000);
		this.dbcur = new Date(Long.parseLong(param[3]) * 1000);
		this.length = Integer.parseInt(param[4]);
	}
	
	// Convert to Network and file
	public String toProtocolString() {
		StringBuilder sb = new StringBuilder();
		sb.append(this.uid);
		sb.append(',');
		sb.append(this.start.getTime()/1000);
		sb.append(',');
		sb.append(this.end.getTime()/1000);
		sb.append(',');
		sb.append(this.dbcur.getTime()/1000);
		sb.append(',');
		sb.append(this.length);
		
		return sb.toString();
	}
	
	public String toUpdateProtocolString() {
		StringBuilder sb = new StringBuilder();
		sb.append(this.uid);
		sb.append(',');
		sb.append(this.start.getTime()/1000);
		sb.append(',');
		sb.append(this.end.getTime()/1000);
		sb.append(',');
		sb.append(this.length);
		
		return sb.toString();
	}
	
	@Override
	public String toString() {
		
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd, HH:mm.");
		String opt = String.format("%s ran %s meters at %s",
				this.uid, 
				String.valueOf(this.length), 
				sdf.format(this.end)
				);
		
		return opt;
	}
	
	
	

}
